import { parseTime } from "./formatDate";


/**
 * 禁用未来时间
 * @param {string} time 组件时间-任意格式的时间
 * @param {number} offsetTime 右偏移量-天数-可为负数
 * @returns boolean
 */
export function disableFutureDate(time, offsetTime = 0) {
    let normalTime = Date.parse(parseTime(time, "{y}-{m}-{d}"));
    let dateNowStart = Date.parse(parseTime(new Date(), "{y}-{m}-{d}")) + (86400000 * offsetTime);

    return normalTime > dateNowStart;
}
/**
 * 开始时间选择限制
 * @param {{
 *  time:number,  
 *  endTime:string, 
 *  offsetTime:number, 
 *  range:number, 
 * }} 
 * time:组件时间-任意格式的时间
 * endTime:任意格式的时间
 * offsetTime:右偏移量-天数-可为负数
 * range:相对日期可选范围-天数-默认31天
 * @returns boolean
 */
export function disabledDateForStartTime({ time, endTime, offsetTime = 0, range = 31 }) {
    let normalStartTime = Date.parse(parseTime(time, "{y}-{m}-{d}"));
    let normalEndTime = endTime && Date.parse(parseTime(endTime, "{y}-{m}-{d}"));
    let dateNowStart = Date.parse(parseTime(new Date(), "{y}-{m}-{d}")) + (86400000 * offsetTime);

    if (!normalEndTime) {
        return normalStartTime > dateNowStart;

    } else {
        let startDate = range ? (normalEndTime - (86400000 * (range - 1))) : 0; // 结束日期前推range天
        let lastTime = normalEndTime;

        if (dateNowStart < normalEndTime) { // 快捷时间选择'本周/本月'后结束时间可能会大于当前时间
            lastTime = dateNowStart
        }

        return (
            normalStartTime < startDate
            || normalStartTime > lastTime
        );
    }
}

/**
 * 结束时间选择限制
 * @param {{
 *  time:number,  
 *  startTime:string, 
 *  offsetTime:number, 
 *  range:number, 
 * }} 
 * time:组件时间-任意格式的时间
 * startTime:任意格式的时间
 * offsetTime:右偏移量-天数-可为负数
 * range:相对日期可选范围-天数-默认31天
 * @returns boolean
 */
export function disabledDateForEndTime({ time, startTime, offsetTime = 0, range = 31 }) {
    let normalEndTime = Date.parse(parseTime(time, "{y}-{m}-{d}"));
    let normalStartTime = startTime && Date.parse(parseTime(startTime, "{y}-{m}-{d}"));
    let dateNowEnd = (Date.parse(parseTime(new Date(), "{y}-{m}-{d}")) + 86400000 - 1) + (86400000 * offsetTime);// 结束时间处理为当天最后一刻

    if (!normalStartTime) {
        return normalEndTime > dateNowEnd;

    } else {
        let deadline = range ? (normalStartTime + (86400000 * (range - 1))) : dateNowEnd; // 开始日期后推range天
        let lastTime = deadline;

        if (dateNowEnd < deadline) {
            lastTime = dateNowEnd
        }

        return (
            normalEndTime < Date.parse(startTime)
            || normalEndTime > lastTime
        );
    }
}

/**
 * 快捷时间 + 偏移量 处理
 * @param {string} timestamp 时间戳
 * @param {number} offsetDay 日期偏移天数（右偏移量）
 * @returns 时间戳
 */
export function normalShortcutTime(timestamp, offsetDay) {
    const targetTime = Date.parse(parseTime(timestamp, "{y}-{m}-{d}"));
    const displayNow = Date.parse(parseTime(Date.now(), "{y}-{m}-{d}"));

    if (targetTime === displayNow) {
        return targetTime + (86400000 * offsetDay);
    } else {
        return targetTime;
    }
}